package cc.sheji.Service;


import cc.sheji.domain.Information;
import cc.sheji.domain.InformationQueryCriteria;
import cc.sheji.reposity.InformationRepository;
import cc.sheji.util.QueryHelp;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.PathVariable;

import java.util.List;

@Service
public class InformationService {

    @Autowired
    InformationRepository informationRepository;

    public Information create(Information entity)  {
        entity.setId(null);
        entity= informationRepository.save(entity);
        return entity;
    }

    public Information get(@PathVariable("id") Long id) {
        Information entity = informationRepository.findById(id).get();
        return entity;
    }

    public List<Information> list(InformationQueryCriteria criteria, Pageable pageable) {
        Sort order = Sort.by(Sort.Direction.DESC, "createTime");
        pageable = PageRequest.of(pageable.getPageNumber(), pageable.getPageSize(), order);
        Page<Information> page = informationRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root, criteria, criteriaBuilder), pageable);
        return page.getContent();
    }

    public Information update(Information job) {
        Information job1 = informationRepository.findById(job.getId()).get();
        return informationRepository.save(job1);
    }

}
